package com.fusion.ui.macos;

import com.fusion.launchers.OSInfo;
import com.fusion.ui.UISubsystem;

import javax.swing.*;


/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 19, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class OSXAdaptation {
    private UISubsystem ui;

    public OSXAdaptation(UISubsystem ui) {
        try {
            this.ui = ui;
            if (OSInfo.isMac()) {
                OSXAdapter.setPreferencesHandler(this, getClass().getMethod("preferences"));
                OSXAdapter.setOpenApplicationHandler(this, getClass().getMethod("show"));
                OSXAdapter.setReOpenApplicationHandler(this, getClass().getMethod("show"));
                OSXAdapter.setQuitHandler(this, getClass().getMethod("quit"));
            }
        } catch(Exception e) {
            ui.handleErrorInEventLoop(e);
        }
    }

    public void preferences() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    ui.getMainWindow().EVENT_options(null);
                } catch (Exception e) {
                    ui.handleErrorInEventLoop(e);
                }
            }
        });
    }

    public void show() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ui.getMainWindow().setVisible(true);
            }
        });
    }

    public void quit() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ui.getCore().shutdown();
            }
        });
    }
}
